/**
 * Project Wonderland
 *
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., All Rights Reserved
 *
 * Redistributions in source code form must reproduce the above
 * copyright and this condition.
 *
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 *
 * Sun designates this particular file as subject to the "Classpath"
 * exception as provided by Sun in the License file that accompanied
 * this code.
 */
package org.jdesktop.wonderland.modules.season.client;

import java.awt.Canvas;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.SwingUtilities;
import org.jdesktop.wonderland.client.BaseClientPlugin;
import org.jdesktop.wonderland.client.cell.Cell;
import org.jdesktop.wonderland.client.hud.HUD;
import org.jdesktop.wonderland.client.hud.HUDComponent;
import org.jdesktop.wonderland.client.hud.HUDManagerFactory;
import org.jdesktop.wonderland.client.input.Event;
import org.jdesktop.wonderland.client.input.EventClassListener;
import org.jdesktop.wonderland.client.jme.JmeClientMain;
import org.jdesktop.wonderland.client.login.ServerSessionManager;
import org.jdesktop.wonderland.client.scenemanager.SceneManager;
import org.jdesktop.wonderland.client.scenemanager.event.HoverEvent;
import org.jdesktop.wonderland.common.annotation.Plugin;

/**
 * A client-side plugin to listen for hover events from the Scene Manager and
 * display a tooltip in the HUD, if the Tooltip Cell Component is present.
 *
 * @author christian
 */
@Plugin
public class SeasonClientPlugin extends BaseClientPlugin {

    // The listener for the Scene Manager hover event
    private TooltipHoverListener listener = null;

    // A HUD Component that holds a tooltip label and the JPanel that holds the
    // label.
    private HUDComponent hudComponent = null;

    /**
     * {@inheritDoc}
     */
    @Override
    public void initialize(ServerSessionManager sessionManager) {
        // Create the listener for the Scene Manager hover events; to be added
        // in activate().
        listener = new TooltipHoverListener();

        super.initialize(sessionManager);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void activate() {

        // Add the tooltip hover listener to the Scene Manager
        SceneManager.getSceneManager().addSceneListener(listener);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void deactivate() {
        
        // Remove the tooltip hover listener from the Scene Manager
        SceneManager.getSceneManager().removeSceneListener(listener);

        // Remove the component from the HUD.
        HUD mainHUD = HUDManagerFactory.getHUDManager().getHUD("main");
        mainHUD.removeComponent(hudComponent);
        hudComponent = null;
    }

    /**
     * Listner for the Scene Manager hover event
     */
    private class TooltipHoverListener extends EventClassListener {

        /**
         * {@inheritDoc}
         */
        @Override
        public Class[] eventClassesToConsume() {
            return new Class[] { HoverEvent.class };
        }
    }
}
